home *** CD-ROM | disk | FTP | other *** search
/ Windows News 2010 Summer - Disc 1 / WN_Ete2010_CD1.iso / Onglet5 / Weezo / Weezo setup.exe / {code_appDir} / www / includes / bmp.php < prev    next >
PHP Script  |  2010-05-19  |  3KB  |  93 lines

  1. <?php
  2. function ImageCreateFromBMP($filename, $isFile=true){
  3.  //Ouverture du fichier en mode binaire
  4.  if($isFile && ! $f1 = fopen($filename,"rb")) return FALSE;
  5.  $pos=0;
  6.  //1 : Chargement des ent?tes FICHIER
  7.    $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", (($isFile)?fread($f1,14):substr($filename,$pos,14)));
  8.    $pos+=14;
  9.    if ($FILE['file_type'] != 19778) return FALSE;
  10.  
  11.  //2 : Chargement des ent?tes BMP
  12.    $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  13.                  '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  14.                  '/Vvert_resolution/Vcolors_used/Vcolors_important', (($isFile)?fread($f1,40):substr($filename,$pos,40)));
  15.    $pos+=40;
  16.    $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  17.    if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  18.    $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  19.    $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  20.    $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  21.    $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  22.    $BMP['decal'] = 4-(4*$BMP['decal']);
  23.    if ($BMP['decal'] == 4) $BMP['decal'] = 0;
  24.  
  25.  //3 : Chargement des couleurs de la palette
  26.    $PALETTE = array();
  27.    if ($BMP['colors'] < 16777216)
  28.    {
  29.        $PALETTE = unpack('V'.$BMP['colors'], (($isFile)?fread($f1,$BMP['colors']*4):substr($filename,$pos,$BMP['colors']*4)));
  30.        $pos+=$BMP['colors']*4;
  31.    }
  32.  
  33.  //4 : Cr?ation de l'image
  34.    $IMG = (($isFile)?fread($f1,$BMP['size_bitmap']):substr($filename,$pos,$BMP['size_bitmap']));
  35.    $pos+=$BMP['size_bitmap'];
  36.    $VIDE = chr(0);
  37.  
  38.    $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  39.    $P = 0;
  40.    $Y = $BMP['height']-1;
  41.    while ($Y >= 0)
  42.    {
  43.    $X=0;
  44.    while ($X < $BMP['width'])
  45.    {
  46.      if ($BMP['bits_per_pixel'] == 24)
  47.        $COLOR = unpack("V",substr($IMG,$P,3).$VIDE);
  48.      elseif ($BMP['bits_per_pixel'] == 16)
  49.      {
  50.        $COLOR = unpack("n",substr($IMG,$P,2));
  51.        $COLOR[1] = $PALETTE[$COLOR[1]+1];
  52.      }
  53.      elseif ($BMP['bits_per_pixel'] == 8)
  54.      {
  55.        $COLOR = @unpack("n",$VIDE.substr($IMG,$P,1));
  56.        $COLOR[1] = $PALETTE[$COLOR[1]+1];
  57.      }
  58.      elseif ($BMP['bits_per_pixel'] == 4)
  59.      {
  60.        $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  61.        if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);
  62.        $COLOR[1] = $PALETTE[$COLOR[1]+1];
  63.      }
  64.      elseif ($BMP['bits_per_pixel'] == 1)
  65.      {
  66.        $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  67.        if    (($P*8)%8 == 0) $COLOR[1] =  $COLOR[1]        >>7;
  68.        elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  69.        elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  70.        elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  71.        elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  72.        elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  73.        elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  74.        elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  75.        $COLOR[1] = $PALETTE[$COLOR[1]+1];
  76.      }
  77.      else
  78.        return FALSE;
  79.      imagesetpixel($res,$X,$Y,$COLOR[1]);
  80.      $X++;
  81.      $P += $BMP['bytes_per_pixel'];
  82.    }
  83.    $Y--;
  84.    $P+=$BMP['decal'];
  85.    }
  86.  
  87.  //Fermeture du fichier
  88.  if($isFile)  fclose($f1);
  89.  
  90.  return $res;
  91. }
  92. ?>
  93.